
/**
 * @author 鍚存倍
 * @since 2022/10/13
 * 12:19
 */
package com.windgo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.windgo.bean.MarketAddress;
import com.windgo.bean.MarketAddressExample;
import com.windgo.bean.MarketUser;
import com.windgo.bean.WxMarketAddress;
import com.windgo.bean.common.BasePageInfo;
import com.windgo.bean.common.CommonData;
import com.windgo.mapper.MarketAddressMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService{
    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Override
    public CommonData<MarketAddress> list(BasePageInfo basePageInfo, Integer id) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        marketAddressExample.setOrderByClause(basePageInfo.getSort()+" "+basePageInfo.getOrder());
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        if(id!=null){
            criteria.andUserIdEqualTo(id);
        }
        List<MarketAddress> list = marketAddressMapper.selectByExample(marketAddressExample);
        PageInfo<MarketAddress> marketAddressPageInfo = new PageInfo<>(list);
        return CommonData.data(marketAddressPageInfo);
    }

    @Override
    public CommonData<MarketAddress> search(BasePageInfo basePageInfo,Integer userId, String name) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        marketAddressExample.setOrderByClause(basePageInfo.getSort()+" "+basePageInfo.getOrder());
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        if(userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if(name!=null&&!"".equals(name)){
            criteria.andNameLike("%"+name+"%");
        }
        List<MarketAddress> list = marketAddressMapper.selectByExample(marketAddressExample);
        PageInfo<MarketAddress> marketAddressPageInfo = new PageInfo<>(list);
        return CommonData.data(marketAddressPageInfo);

    }

    @Override
    public CommonData<WxMarketAddress> wxList(Integer id) {
       /* Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser primaryPrincipal = (MarketUser)principals.getPrimaryPrincipal();
        Integer id1 = primaryPrincipal.getId();

        */
       // PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        if(id!=null){
            criteria.andUserIdEqualTo(id);
        }
       // long l = marketAddressMapper.countByExample(marketAddressExample);
        ArrayList<WxMarketAddress> list = new ArrayList<>();
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);
        for (MarketAddress marketAddress : marketAddresses) {
            WxMarketAddress wxMarketAddress = new WxMarketAddress(marketAddress.getId(), marketAddress.getName(), marketAddress.getUserId(), marketAddress.getProvince(), marketAddress.getCity(), marketAddress.getCounty(), marketAddress.getAddressDetail(), marketAddress.getAreaCode(), marketAddress.getTel(), marketAddress.getIsDefault(), marketAddress.getAddTime(), marketAddress.getUpdateTime(), marketAddress.getDeleted());
            list.add(wxMarketAddress);

        }
        PageInfo<WxMarketAddress> wxMarketAddressPageInfo = new PageInfo<>(list);
        return CommonData.data(wxMarketAddressPageInfo);
    }

    @Override
    public Integer delete(Integer id) {
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        if(id!=null){
            criteria.andIdEqualTo(id);
        }
        int i = marketAddressMapper.deleteByExample(marketAddressExample);
        return i;
    }

    @Override
    public WxMarketAddress selectById(Integer id) {
        MarketAddress marketAddress = marketAddressMapper.selectByPrimaryKey(id);
        WxMarketAddress wxMarketAddress = new WxMarketAddress(marketAddress.getId(), marketAddress.getName(), marketAddress.getUserId(), marketAddress.getProvince(), marketAddress.getCity(), marketAddress.getCounty(), marketAddress.getAddressDetail(), marketAddress.getAreaCode(), marketAddress.getTel(), marketAddress.getIsDefault(), marketAddress.getAddTime(), marketAddress.getUpdateTime(), marketAddress.getDeleted());
        return wxMarketAddress;
    }

    @Override
    public Integer update(MarketAddress marketAddress, Integer userId) {
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser primaryPrincipal = (MarketUser) principals.getPrimaryPrincipal();
        Integer id = primaryPrincipal.getId();
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        criteria.andUserIdEqualTo(id);
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);
        if(marketAddresses.size()==0){
            Date date = new Date();
            marketAddress.setUpdateTime(date);
            marketAddress.setUserId(id);
            marketAddress.setAddTime(date);
            int i1 = marketAddressMapper.insertSelective(marketAddress);
            if(i1!=1){
                return null;
            }
            return marketAddress.getId();
        }
        if(marketAddress.getId()!=0){
            if(marketAddress.getIsDefault().equals(true)){
                for (MarketAddress address : marketAddresses) {
                    if(address.getIsDefault().equals(true)&&address.getId()!= marketAddress.getId()){
                        Date date = new Date();
                        address.setIsDefault(false);
                        address.setUpdateTime(date);
                        marketAddressMapper.updateByPrimaryKeySelective(address);
                    }
                }
                Date date = new Date();
                //marketAddress.setIsDefault(false);
                marketAddress.setUpdateTime(date);
                marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
                return marketAddress.getId();
            }
            Date date = new Date();
           // marketAddress.setIsDefault(false);
            marketAddress.setUpdateTime(date);
            marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
            return marketAddress.getId();
        }
        if(marketAddresses.size()!=0&&marketAddress.getIsDefault().equals(true)){
            for (MarketAddress address : marketAddresses) {
                if(address.getIsDefault().equals(true)){
                    Date date = new Date();
                    address.setIsDefault(false);
                    address.setUpdateTime(date);
                    marketAddressMapper.updateByPrimaryKeySelective(address);
                }
            }
            Date date = new Date();
            marketAddress.setUpdateTime(date);
            marketAddress.setAddTime(date);
            int i = marketAddressMapper.insertSelective(marketAddress);
            return i;
        }
        Date date = new Date();
        marketAddress.setUpdateTime(date);
        marketAddress.setAddTime(date);
        int i = marketAddressMapper.insertSelective(marketAddress);
        return i;
        /*if(marketAddress.getIsDefault().equals(true)){
            MarketAddressExample marketAddressExample1 = new MarketAddressExample();
            MarketAddressExample.Criteria criteria1 = marketAddressExample1.createCriteria();
            criteria1.andUserIdEqualTo(id);
            List<MarketAddress> marketAddresses1 = marketAddressMapper.selectByExample(marketAddressExample1);
            for (MarketAddress address : marketAddresses1) {
                if(address.getIsDefault().equals(true)){
                    Integer id1 = address.getId();
                    MarketAddress marketAddress1 = marketAddressMapper.selectByPrimaryKey(id1);
                    marketAddress1.setIsDefault(false);
                    int i = marketAddressMapper.updateByPrimaryKey(marketAddress1);
                }
            }
            Date date = new Date();
            marketAddress.setUpdateTime(date);
            marketAddress.setUserId(id);
            marketAddress.setAddTime(date);
            int i1 = marketAddressMapper.insertSelective(marketAddress);
            if(i1!=1){
                return null;
            }
            return marketAddress.getId();
        }
        Date date = new Date();
        marketAddress.setAddTime(date);
        marketAddress.setUpdateTime(date);
        marketAddress.setIsDefault(false);
        int i = marketAddressMapper.insertSelective(marketAddress);
        if(i!=1){
            return null;
        }
        return marketAddress.getId();

         */
    }

}
